﻿Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class admin_templates_edit
        Inherits ActiveAdminBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Utilities.BindEnum(drpTemplateType, GetType(Templates.TemplateTypes), String.Empty, False, True, 0)
            drpTemplateType.Attributes.Add("onchange", "toggleTextTab()")
            If Not Params = String.Empty And Not Params = "undefined" Then
                Try
                    LoadForm(CInt(Params))
                Catch ex As Exception

                End Try
            Else
                btnDelete.Visible = False
            End If
        End Sub
        Private Sub LoadForm(ByVal TemplateId As Integer)

            Dim ti As TemplateInfo
            Dim tc As New TemplateController
            ti = tc.Template_Get(TemplateId, PortalId, ModuleId)
            If Not ti Is Nothing Then
                txtTitle.Text = ti.Title
                txtSubject.Text = ti.Subject
                txtPlainText.Text = ti.TemplateText
                txtEditor.Text = Server.HtmlDecode(ti.TemplateHTML.Replace("[RESX:", "[TRESX:"))
                drpTemplateType.SelectedIndex = drpTemplateType.Items.IndexOf(drpTemplateType.Items.FindByValue(CStr(Convert.ToInt32([Enum].Parse(GetType(Templates.TemplateTypes), ti.TemplateType.ToString())))))
                hidTemplateId.Value = CStr(ti.TemplateId)
                If ti.IsSystem Then
                    btnDelete.Visible = False
                    txtTitle.ReadOnly = True
                    drpTemplateType.Enabled = False
                End If
            End If
        End Sub

        Private Sub cbAction_Callback(ByVal sender As Object, ByVal e As Modules.ActiveForums.Controls.CallBackEventArgs) Handles cbAction.Callback
            Dim sMsg As String = ""
            Select Case e.Parameters(0).ToLower
                Case "save"
                    Try
                        'save template
                        Dim ti As TemplateInfo
                        Dim tc As New TemplateController
                        Dim templateId As Integer = 0
                        If e.Parameters(1).ToString <> "" Then
                            templateId = CInt(e.Parameters(1))
                            ti = tc.Template_Get(templateId, PortalId, ModuleId)
                        Else
                            ti = New TemplateInfo
                            ti.IsSystem = False
                            ti.TemplateType = CType(e.Parameters(6), Templates.TemplateTypes)
                            ti.PortalId = PortalId
                            ti.ModuleId = ModuleId
                        End If
                        ti.Title = e.Parameters(2).ToString
                        ti.Subject = e.Parameters(3).ToString
                        If ti.TemplateType = Templates.TemplateTypes.Email Or ti.TemplateType = Templates.TemplateTypes.ModEmail Or ti.TemplateType = Templates.TemplateTypes.MailConnector Then
                            ti.Template = "<template><html>" & Server.HtmlEncode(e.Parameters(4)) & "</html><plaintext>" & Utilities.StripHTMLTag(e.Parameters(5).ToString) & "</plaintext></template>"
                        Else
                            ti.Template = "<template><html>" & Server.HtmlEncode(e.Parameters(4)) & "</html><plaintext>" & String.Empty & "</plaintext></template>"
                        End If

                        ti.Template = ti.Template.Replace("[TRESX:", "[RESX:")
                        templateId = tc.Template_Save(ti)
                        Dim ckey As String = ModuleId & templateId & Convert.ToString([Enum].Parse(GetType(Templates.TemplateTypes), ti.TemplateType.ToString()))
                        DataCache.CacheClear(ckey)
                        sMsg = "Template saved successfully!"
                    Catch ex As Exception
                        sMsg = "Error saving template."

                    End Try

                Case "delete"
                    Try
                        'delete template
                        Dim ti As TemplateInfo
                        Dim tc As New TemplateController
                        Dim templateid As Integer = 0
                        If e.Parameters(1).ToString <> "" Then
                            templateid = CInt(e.Parameters(1))
                            ti = tc.Template_Get(templateid, PortalId, ModuleId)
                            If Not ti.IsSystem = True Then
                                tc.Template_Delete(templateid, PortalId, ModuleId)
                                sMsg = "Template deleted successfully!"
                            Else
                                sMsg = "Enable to delete system templates"
                            End If
                        End If
                    Catch ex As Exception
                        sMsg = "Error deleting template."
                    End Try

            End Select
            cbActionMessage.InnerText = sMsg
            cbActionMessage.RenderControl(e.Output)
        End Sub
    End Class
End Namespace